package com.titar.robot.ui.welcom;


import com.google.gson.Gson;
import com.titar.robot.AppExecutors;
import com.titar.robot.base.BaseModel;
import com.titar.robot.db.CacheTableDao;
import com.titar.robot.entity.CacheTableData;
import com.titar.robot.entity.MemberInfo;
import com.titar.robot.http.ApiResponse;
import com.titar.robot.http.service.CustomerService;
import com.titar.robot.repository.NetWorkBoundResource;
import com.titar.robot.repository.Resource;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import static com.titar.robot.Constants.LOGIN_URL;
import static com.titar.robot.Constants.MEMBERINFO_URL;
import static com.titar.robot.Constants.RET_SUCCESS_0;

public class WelcomeRepository {

    private AppExecutors appExecutors;

    private CacheTableDao cacheTableDao;

    private CustomerService customerService;


    public WelcomeRepository(AppExecutors appExecutors, CacheTableDao cacheTableDao, CustomerService customerService) {
        this.appExecutors = appExecutors;
        this.cacheTableDao = cacheTableDao;
        this.customerService = customerService;

    }

    public LiveData<Resource<MemberInfo>> checkToken(String token) {
        return new NetWorkBoundResource<MemberInfo, BaseModel<MemberInfo>>(appExecutors) {

            @Override
            public void onFetchFailed() {
                super.onFetchFailed();
            }

            @Override
            protected void saveCallResult(BaseModel item) {
                if (item.getErrcode() == RET_SUCCESS_0) {
                    //保存数据
                    CacheTableData cacheTableData = new CacheTableData();
                    cacheTableData.setKey(BaseModel.genKey(MEMBERINFO_URL, ""));
                    Gson gson = new Gson();
                    cacheTableData.setValue(gson.toJson(item.getData()));
                    cacheTableDao.insert(cacheTableData);
                }
            }

            @Override
            protected Boolean shouldFetch(MemberInfo data) {
                return true;
            }

            @Override
            protected LiveData<MemberInfo> loadFromDb() {
                MediatorLiveData<MemberInfo> result = new MediatorLiveData();
                cacheTableDao.get(BaseModel.genKey(MEMBERINFO_URL, "")).observeForever(new Observer<CacheTableData>() {
                    @Override
                    public void onChanged(CacheTableData cacheTableData) {
                        Gson gson = new Gson();
                        if (cacheTableData != null) {
                            result.setValue(gson.fromJson(cacheTableData.getValue(), MemberInfo.class));
                        } else {
                            result.setValue(gson.fromJson("", MemberInfo.class));
                        }
                    }
                });
                return result;
            }

            @Override
            protected LiveData<ApiResponse<BaseModel<MemberInfo>>> createCall() {
                return customerService.getMemberInfo(token);
            }
        }.asLiveData();
    }

}
